package uk.co.hephistocles.blobGame;


import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class PathFollower {
	private Character 	sprite;
    private Vector2		pos;
    private int			totTime;
    private Path 		path;
    private int			runningTime;
    private int			curveTime;
    private boolean		followAngle;
    public boolean		finished;
	
    public PathFollower(Character sprite, Path pat, int totTime) {
    	this.sprite = sprite;
    	this.totTime = totTime;
    	runningTime = 0;
    	finished = false;
    	this.path = pat;
    	curveTime = totTime/(path.getSegmentsNum());
    	pos = new Vector2();
    }

    public void setFollowAngle(boolean set) {
    	followAngle = true;
    }
    
    public void setOrigin(float x, float y) {
    	sprite.setOrigin(x, y);
    }
    public void setRotation(float a) {
    	sprite.setRotation(a);
    }
    
    public void update(int dt) {
    	if (runningTime/curveTime < path.getSegmentsNum()) {
    		if (runningTime>0) {
		    	int segment = (int) Math.floor(runningTime/curveTime);
		    	//System.out.println(runningTime +"," + 1.0f*(runningTime%curveTime)/curveTime + ", " + curveNum);
		    	Vector3 p = path.getPos(segment, 1.0f*(runningTime%curveTime)/curveTime);
		    	setRotation((float) ((180/Math.PI)*Math.atan2(pos.y - p.y, pos.x - p.x)) + 90);
		    	pos.y =(int) p.y;
		    	pos.x =(int) p.x;
    		}
    	} else {
    		finished = true;
    	}
    	runningTime += dt;
    	sprite.update();
    }
	
	public void draw() {
		if (!finished) {
			sprite.draw((int)pos.x,(int) pos.y);
		}
	}
}
